/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sorting;

/**
 *
 * @author Akram Mellice
 */
public final class SelectionSort<T extends Comparable> extends Sort<T> {

    public SelectionSort(T[] array, Class<T> classType) {
        super(array, classType);
    }

    private int maxElement(int end) {
        T max = array[0];
        int maxIndex = 0;
        for (int i = 1; i < end; i++) {
            if (array[i].compareTo(max) > 0) {
                maxIndex = i;
                max = array[i];
            }
        }
        return maxIndex;
    }

    /**
     * Sort using Selection Sort algorithms
     * O(n^2)
     */
    @Override
    public void sort() {
        int max = 0, currentLength = array.length;
        for (int i = 0; i < array.length; i++) {
            max = maxElement(currentLength--);
            T tmp = array[currentLength];
            array[currentLength] = array[max];
            array[max] = tmp;
        }
    }
}
